package ManageGameHServer;

import ManageGameHServer.classFile.*;
import ManageGameHServer.code.*;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;

import ManageGameHServer.conf.Setup;

/**
 * Created by lt on 2016/8/27.
 */
public class ExeServer {
	public static final void  main(String[] args) throws Exception{
		System.setProperty("user.timezone","GMT+08");                      //设置时区
		int port= Setup.serverPort;
		if(args.length>=1){
			port=Integer.valueOf(args[0]);
			Setup.serverPort=Integer.valueOf(args[0]);
		}
		EventLoopGroup boss=new NioEventLoopGroup();
		EventLoopGroup work=new NioEventLoopGroup();
		ServerBootstrap bootstrap=new ServerBootstrap();
		bootstrap.group(boss,work).channel(NioServerSocketChannel.class).childHandler(new HttpServerHandler());
		//数据库连接初始化
		DBConnectO.Initialization();                     //数据库连接初始化

		//日志数据库连接初始化
		LogDBConnectO.Initialization();                     //日志数据库连接初始化
		//nosql连接初始化
		NoDBConnectO.Initialization();                   //nosql数据库连接初始化

		//启动日志消息监听
		new LogicGameServerWorker().start();

		//获取服务器启动时间
		Date date=new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		long nowTime=System.currentTimeMillis();
		String nowDay = dateFormat.format(nowTime);                  //今天
		long nowDayStarteTime = dateFormat.parse(nowDay).getTime();                        //今天开始时间戳
		try {
			File f = new File(ExeServer.class.getResource("").getPath());
			System.out.println(f);
			Timer timer = new Timer();
			MyTimerTask myTimerTask = new MyTimerTask();
/*
			timer.schedule(myTimerTask,1*6*60*60*1000-1*60*1000,1*24*60*60*1000);
*/

			Calendar calendar = Calendar.getInstance();
			calendar.set(Calendar.HOUR_OF_DAY, 24); // 24点  可以更改时间
			calendar.set(Calendar.MINUTE, 0); // 0秒 可以更改分数
			calendar.set(Calendar.SECOND, 0);// 0  默认为0,不以秒计
			Date date1 = calendar.getTime();
			long time = 24 * 60 * 60 * 1000;//24个小时
			timer.schedule(myTimerTask, date1, time);

			DBSwitch.DBSwitchCheck();

			ChannelFuture fb = bootstrap.bind(port).sync();
			System.out.println("************************************************************");
			System.out.println("ManageGameHServer is worker,this port is "+port+", this is running .....");
			System.out.println("************************************************************");
			fb.channel().closeFuture().sync();
		}finally {
			boss.shutdownGracefully();
			work.shutdownGracefully();
		}
	}
}